#!/bin/bash
# -*- mode: sh -*-

source scripts/include/header
source scripts/include/function

if [[ -f "support/${PROJECT_NAME}/LANG" ]]
then
    MAINLANG=$(cat "support/${PROJECT_NAME}/LANG")
elif [[ -f "support/${PROJECT_NAME}/MAINLANG" ]]
     then
    MAINLANG=$(cat "support/${PROJECT_NAME}/MAINLANG")
else
    MAINLANG=english
fi

if [[ -f "support/${PROJECT_NAME}/ALTLANG" ]]
then
    ALTLANG=$(cat "support/${PROJECT_NAME}/ALTLANG")
else
    case ${MAINLANG} in
	english)
	    ALTLANG=russian
	    ;;
	russian)
	    ALTLANG=english
	    ;;
	*)
	    ALTLANG=russian
	    ;;
    esac
fi

if [[ -f "support/${PROJECT_NAME}/TWOLANG" ]]
then
    LANGLIST="${MAINLANG} ${ALTLANG}"
else
    LANGLIST=${MAINLANG}
fi

if [[ -f "support/${PROJECT_NAME}/TWOFILES" ]]
then
    TWOFILES=yes
else
    TWOFILES=no
fi

# For specific project
if [[ -f "scripts/include/project/${PROJECT_NAME}" ]]
then
    source "scripts/include/project/${PROJECT_NAME}"
fi

## Variables
out_name=${OUT_NAME}
out_dir=${OUT_DIR}

# {{{ Make preamble

for lang in ${LANGLIST}
do
    [[ $TWOFILES == yes ]] && out_dir="${OUT_DIR}/${lang}"

    mkdir -p ${out_dir}/{title,text,bib,image}

    touch ${out_dir}/${lang}.tex

    cat support/${PROJECT_NAME}/preamble/class/*.tex >> ${out_dir}/${lang}.tex
    cat support/${PROJECT_NAME}/preamble/lang/${lang}.tex >> ${out_dir}/${lang}.tex
    cat support/${PROJECT_NAME}/preamble/pkg/*.tex >> ${out_dir}/${lang}.tex

    cp ${lang}.bbl ${out_dir}/

    cat ${out_dir}/${lang}.tex >> ${out_dir}/text/${out_name}.tex

    # Копируем библиографию
    cp -r bib/* ${out_dir}/bib/
done

# }}}

# {{{ title

for file in "$@"
do
    # Из head берём только основной язык
    if [[ $file == +(title/*head*) ]]
    then

	for lang in ${LANGLIST}
	do
	    [[ $TWOFILES == yes ]] && out_dir="${OUT_DIR}/${lang}"
	
	    clear_all_lang
	    
	    cat ./${out_dir}/${lang}_clean.tex >> ./${out_dir}/${file}
	    rm ./${out_dir}/*.tex
	done
	
	shift
	continue
    fi

    # Join all authors
    if [[ $file == +(title/*author*) ]]
    then

	for lang in ${LANGLIST}
	do
	    [[ $TWOFILES == yes ]] && out_dir="${OUT_DIR}/${lang}"

	
	    clear_all_lang

	    cat ./${out_dir}/${lang}_clean.tex >> ./${out_dir}/${file%%_*}.tex

	    rm ./${out_dir}/*.tex

	done
	
	shift
	continue
    fi

    if [[ $file == +(title/*) ]]
    then


	for lang in ${LANGLIST}
	do
	    [[ $TWOFILES == yes ]] && out_dir="${OUT_DIR}/${lang}"

	    clear_all_lang
	    
	    # clear_maintitle
	    # clear_alttitle

	    # Только основной язык
	    cat ./${out_dir}/${lang}_clean.tex >> ./${out_dir}/${file}

	    rm ./${out_dir}/*.tex

	done
	
	shift
    fi
done

for lang in ${LANGLIST}
do
    [[ $TWOFILES == yes ]] && out_dir="${OUT_DIR}/${lang}"

    cp support/${PROJECT_NAME}/title/*.tex ${out_dir}/title/
    cat ./${out_dir}/title/*.tex >> ./${out_dir}/text/${out_name}.tex
    rm -rvf ./${out_dir}/title

done

# }}}

# {{{ body

for file in "$@"
do

    for lang in ${LANGLIST}
    do
	[[ $TWOFILES == yes ]] && out_dir="${OUT_DIR}/${lang}"
    
	clear_all_lang
		
	# Нужен только один язык
	cat ./${out_dir}/${lang}_clean.tex >> ./${out_dir}/text/${out_name}.tex
	
	rm ./${out_dir}/*.tex

    done
done

for lang in ${LANGLIST}
do
    [[ $TWOFILES == yes ]] && out_dir="${OUT_DIR}/${lang}"

    clear_comment "${out_dir}/text/${out_name}.tex"
done
    
# }}}

# {{{ Make postamble

for lang in ${LANGLIST}
do
    [[ $TWOFILES == yes ]] && out_dir="${OUT_DIR}/${lang}"

    cat support/${PROJECT_NAME}/preamble/end/* >> ${out_dir}/text/${out_name}.tex

    clear_garbage
done

# }}}

# {{{ Copy images

cp -R image "${out_dir}"

# }}}

# {{{ Copy support files

if [[ -d "support/${PROJECT_NAME}/files" ]]
then
    if [[ ! -z `ls support/"${PROJECT_NAME}"/files/` ]]
    then
	cp -R support/"${PROJECT_NAME}"/files/* "${out_dir}"/text/
    fi
fi

# }}}

# {{{ Project specific hack

project_hack

# }}}

